package com.sxt;

import connect.Cliant;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.sql.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//交互式对话框，对应登录和注册两个界面
public class MyInteractDialog extends JDialog {

    Container container;
    int width;
    int height;
    String title;
    String field01;
    String field02;
    String option01;
    String option02;
    static int num1 = 0;//记录鼠标点击眼睛次数
    static int num2 = 0;//记录鼠标点击眼睛次数
    static int num3 = 0;//记录鼠标点击眼睛次数

    static int count1=0;//记录输入密码的位数
    static int i=3;//记录登录失败次数
    String option03;
    public static JTextField userNameTextField = new JTextField(10);
    public static JPasswordField passwordTextField = new JPasswordField(10);
    JPasswordField passwordConformField = new JPasswordField(10);

    public MyInteractDialog() {
    }

    public MyInteractDialog(int width, int height, String title, String field01, String field02, String option01, String option02,String option03) {
        this.width = width;
        this.height = height;
        this.title = title;
        this.field01 = field01;
        this.field02 = field02;
        this.option01 = option01;
        this.option02 = option02;
        this.option03 = option03;
        this.container = this.getContentPane();
        this.setSize(width, height);
        this.setTitle(title);
        this.setLocationRelativeTo(null);

    }

    public void showDialogRegister() {
        JLabel label01 = new JLabel(field01);
        JLabel label02 = new JLabel(field02);

        JButton button01 = new JButton(option01);
        JButton button02 = new JButton(option02);
        JButton button03 = new JButton(option03,new ImageIcon("images/eye_1.png"));

        label01.setBounds(20,25,40,25);
        userNameTextField.setBounds(65,25,95,25);
        label02.setBounds(20,60,40,25);
        passwordTextField.setBounds(65,60,70,25);
        passwordTextField.setEchoChar('*');
        button01.setBounds(15,100,70,30);
        button02.setBounds(95,100,70,30);
        button03.setBounds(135,60,25,25);

        button01.addActionListener(new MyActionListener(option01, this, button01)); //这两行完了需要修改
        button02.addActionListener(new MyActionListener(option02, this, button02));
        button03.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                num1++;
                button03.setIcon(new ImageIcon("images/eye_"+(num1%2+1)+".png"));
                if(num1%2==1){
                    passwordTextField.setEchoChar('\0');
                }else{
                    passwordTextField.setEchoChar('*');
                }
            }
        });

        this.setLayout(null);
        //this.setLayout(new FlowLayout()); //流式布局

        container.add(label01);
        container.add(userNameTextField);
        container.add(label02);
        container.add(passwordTextField);
        container.add(button01);
        container.add(button02);
        container.add(button03);

        this.setVisible(true); //setVisible需要放到后面
    }
    public void showDialogEnroll() {
        JLabel label01 = new JLabel(field01);
        JLabel label02 = new JLabel(field02);
        JLabel label03 = new JLabel("确认密码");
        JLabel label04 = new JLabel("不得少于四位");
        label04.setFont(new Font("宋体",Font.PLAIN,10));
        label04.setForeground(Color.red);
        JLabel label05 = new JLabel("密码不一致");
        label05.setFont(new Font("宋体",Font.PLAIN,10));
        label05.setForeground(Color.red);

        JButton button01 = new JButton(option01);
        JButton button02 = new JButton(option02);
        JButton button03 = new JButton(option03,new ImageIcon("images/eye_1.png"));
        JButton button04 = new JButton(option03,new ImageIcon("images/eye_1.png"));

        label01.setBounds(50,30,40,25);
        userNameTextField.setBounds(105,30,115,25);
        label02.setBounds(50,65,40,25);
        passwordTextField.setBounds(105,65,90,25);
        passwordTextField.setEchoChar('*');
        passwordTextField.getDocument().addDocumentListener(new DocumentListener(){

            @Override
            public void insertUpdate(DocumentEvent e) {
                count1++;
                if(count1<=4){
                    label04.setText("不得少于四位");
                    label04.setForeground(Color.red);
                }else{
                    if(parsePassword(passwordTextField.getText())==3){
                        label04.setText("强密码");
                        label04.setForeground(Color.green);
                    }else if(parsePassword(passwordTextField.getText())==2) {
                        label04.setText("中强密码");
                        label04.setForeground(Color.green);
                    }else{
                        label04.setText("弱密码");
                        label04.setForeground(Color.red);
                    }
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                count1--;
                if(count1<=4){
                    label04.setText("不得少于四位");
                    label04.setForeground(Color.red);
                }else{
                    if(parsePassword(passwordTextField.getText())==3){
                        label04.setText("强密码");
                        label04.setForeground(Color.green);
                    }else if(parsePassword(passwordTextField.getText())==2) {
                        label04.setText("中强密码");
                        label04.setForeground(Color.green);
                    }else{
                        label04.setText("弱密码");
                        label04.setForeground(Color.red);
                    }
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {

            }
        });

        label03.setBounds(50,100,55,25);
        label04.setBounds(225,65,70,25);
        label05.setBounds(225,90,70,50);
        passwordConformField.setBounds(105,100,90,25);
        passwordConformField.setEchoChar('*');
        passwordConformField.getDocument().addDocumentListener(new DocumentListener(){

            @Override
            public void insertUpdate(DocumentEvent e) {
                if(passwordTextField.getText().equals(passwordConformField.getText())){
                    label05.setText("密码一致");
                    label05.setForeground(Color.green);
                }else{
                    label05.setText("密码不一致");
                    label05.setForeground(Color.red);
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                if(passwordTextField.getText().equals(passwordConformField.getText())){
                    label05.setText("密码一致");
                    label05.setForeground(Color.green);
                }else{
                    label05.setText("密码不一致");
                    label05.setForeground(Color.red);
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {

            }
        });
        button01.setBounds(65,150,70,30);
        button02.setBounds(165,150,70,30);
        button03.setBounds(195,65,25,25);
        button04.setBounds(195,100,25,25);

        button01.addActionListener(new MyActionListener(option01, this, button01)); //这两行完了需要修改
        button02.addActionListener(new MyActionListener(option02, this, button02));
        button03.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                num2++;
                button03.setIcon(new ImageIcon("images/eye_"+(num2%2+1)+".png"));
                if(num2%2==1){
                    passwordTextField.setEchoChar('\0');
                }else{
                    passwordTextField.setEchoChar('*');
                }
            }
        });
        button04.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                num3++;
                button04.setIcon(new ImageIcon("images/eye_"+(num3%2+1)+".png"));
                if(num3%2==1){
                    passwordConformField.setEchoChar('\0');
                }else{
                    passwordConformField.setEchoChar('*');
                }
            }
        });

        this.setLayout(null);
        //this.setLayout(new FlowLayout()); //流式布局

        container.add(label01);
        container.add(userNameTextField);
        container.add(label02);
        container.add(passwordTextField);
        container.add(label03);
        container.add(passwordConformField);
        container.add(label04);
        container.add(label05);
        container.add(button01);
        container.add(button02);
        container.add(button03);
        container.add(button04);

        this.setVisible(true); //setVisible需要放到后面
    }
    public int parsePassword(String password) {
        int result = 0;
        Pattern ps = Pattern
                .compile("[a-zA-Z0-9_\\,\\.\\-\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\+]{6,16}");
        Pattern lower = Pattern.compile("[a-z]");
        Matcher mlower = lower.matcher(password);
        Pattern upper = Pattern.compile("[A-Z]");
        Matcher mupper = upper.matcher(password);
        Pattern special = Pattern
                .compile("[_\\,\\.\\-\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\+]");
        Matcher mspecial = special.matcher(password);
        if (ps.matcher(password).matches()) {
            if (mspecial.find()) {
                result = 3;
            } else if (mupper.find()) {
                result = 2;
            } else if (mlower.find()) {
                result = 1;
            }
        }
        return result;
    }
    //内部类
    class MyActionListener implements ActionListener {
        //这几行是我用来测试的!
        String message;
        MyInteractDialog interactDialog;
        JButton button;

        public MyActionListener(String message, MyInteractDialog interactDialog, JButton button) {
            this.message = message;
            this.interactDialog = interactDialog;
            this.button = button;
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            if (title.equals("登录")) {
                if(button.getText().equals("登录")) {
                    Cliant cliant=new Cliant();
                    //填写用户名和密码

                    //用户信息和URL
//                    String url="jdbc:mysql://localhost:3306/runoob?useUnicode=true&characterEncoding=utf8&useSSL=true";
//                    String username="root";
//                    String password="261043yy";
                    //连接成功，数据库对象  Connection代表数据库
//                    Connection connection = null;
                    try {
                        int one =cliant.login(userNameTextField.getText(),passwordTextField.getText());
//                        connection = DriverManager.getConnection(url, username, password);

                        //执行SQL对象  statement 执行sql对象
//                        Statement statement = connection.createStatement();

                        //执行SQL对象 去执行SQL，可能存在结果，查看返回结果
//                        String sql="select *from User where User.name='"+userNameTextField.getText()+"' and User.password='"+passwordTextField.getText()+"'";
//                        ResultSet resultSet = statement.executeQuery(sql);//返回结果集,结果封装了我们全部查询的结果
                        if (one==1) {
                            //如果检测正确
                            JOptionPane.showMessageDialog(interactDialog, "登录成功！", "提示", 1);
                            interactDialog.dispose();
                            GameWin.haveLoggedIn = true;
                        }
                        if(i==0||i==2)
                        {
                            if(i==0){
//                                connection.close();
//                                statement.close();
//                                resultSet.close();
                                JOptionPane.showMessageDialog(interactDialog, "用户已锁定！", "提示", 0);
                            }else{
                                JOptionPane.showMessageDialog(interactDialog, "用户名或密码错误！"+'\n'+"您还有"+i+"次输入机会！", "提示", 0);
                                i--;
                            }
                        }
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }


                }else if(button.getText().equals("退出")){
                    //关闭弹窗
                    interactDialog.dispose();
                }
            } else if (title.equals("注册")) {
                if(button.getText().equals("注册")) {
                    Cliant cliant=new Cliant();
                    //注册用户名和密码
//                    String url="jdbc:mysql://localhost:3306/runoob?useUnicode=true&characterEncoding=utf8&useSSL=true";
//                    String username="root";
//                    String password="261043yy";
                    //连接成功，数据库对象  Connection代表数据库
//                    Connection connection = null;
                    try {

//                        cliant.enroll(userNameTextField.getText(),passwordTextField.getText());
//                        connection = DriverManager.getConnection(url, username, password);
//                        Statement statement = connection.createStatement();
//                        String sql1="select *from User where User.name='"+userNameTextField.getText()+"' and User.password='"+passwordTextField.getText()+"'";
//                        ResultSet resultSet = statement.executeQuery(sql1);//返回结果集,结果封装了我们全部查询的结果
                        int one=cliant.enroll(userNameTextField.getText(),passwordTextField.getText());

                        if (one==1||one==0) {
                            //如果检测正确
                            JOptionPane.showMessageDialog(interactDialog, "用户名密码已存在！", "提示", 1);
                            interactDialog.dispose();
                        }
                        if(one==2){

//                            String sql = "insert ignore into User values(?,?)";
//                            connection = DriverManager.getConnection(url, username, password);
//                            PreparedStatement pstmt = connection.prepareStatement(sql);
//                            pstmt.setString(1, userNameTextField.getText());
//                            pstmt.setString(2, passwordTextField.getText());
//                            pstmt.executeUpdate();

                            System.out.println("已经输入注册的用户名密码，正在进行检测。。。");
                            JOptionPane.showMessageDialog(interactDialog, "注册成功！", "提示", 1);
                            interactDialog.dispose();
                            GameWin.haveLoggedIn = true;
                        }


                    }catch(IOException ex) {
                        throw new RuntimeException(ex);
                    }
                }else if(button.getText().equals("退出")){
                    //退出
                    interactDialog.dispose();
                }
            }
        }
    }
}
